<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${package}.${moduleName}.dao.${className}Dao">

    <resultMap id="${className}Map" type="${package}.${moduleName}.model.${className}">
        <#if columns?? &&columns?size gt 0>
            <#list columns as column>
        <result column="${column.columnName}" jdbcType="${column.dataType}" property="${column.attrname}" />
            </#list>
        </#if>
    </resultMap>

    <sql id="allColumns">
        <#if columns?? &&columns?size gt 0>
            <#list columns as column>
        <#if column_index+1<columns?size>${column.columnName},<#else>${column.columnName}</#if>
            </#list>
        </#if>
    </sql>

    <sql id="dynamicWhere">
        <trim  suffixOverrides="," prefix="WHERE" prefixOverrides="AND">
            <#if columns?? &&columns?size gt 0>
                <#list columns as column>
                    <#if column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType=='String' && pk.columnName?? && pk.columnName == column.columnName>
            <if test="${column.attrname}!=null and ${column.attrname}!=''">AND ${column.columnName} = <#noparse>#{</#noparse>${column.attrname}<#noparse>}</if></#noparse>
                    <#elseif column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType=='String' && pk.columnName?? && pk.columnName!=column.columnName>
            <if test="${column.attrname}!=null and ${column.attrname}!=''">AND ${column.columnName} LIKE concat('%', <#noparse>#{</#noparse>${column.attrname}<#noparse>} ,'%')</if></#noparse>
                    <#elseif column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType!='String' && pk.columnName?? && pk.columnName == column.columnName>
            <if test="${column.attrname}!=null ">AND ${column.columnName} = <#noparse>#{</#noparse>${column.attrname}<#noparse>}</if></#noparse>
                    <#elseif column.attrname?? && column.attrname!=null && column.attrname!="" && column.attrType?? && column.attrType!='String' && pk.columnName?? && pk.columnName != column.columnName>
            <if test="${column.attrname}!=null ">AND ${column.columnName} = <#noparse>#{</#noparse>${column.attrname}<#noparse>}</if></#noparse>
                    </#if>
                </#list>
            </#if>
        </trim>
    </sql>

    <!-- select -->
    <select id="select" parameterType="${package}.${moduleName}.model.${className}"  resultMap="${className}Map">
        SELECT <include refid="allColumns" />
        FROM ${tableName}  <include refid="dynamicWhere" />
    </select>

    <!-- view -->
    <select id="view" parameterType="${pk.propType}"   resultMap="${className}Map">
        SELECT <include refid="allColumns" />
        FROM ${tableName} WHERE ${pk.columnName} =  <#noparse>#{</#noparse>${pk.attrname}<#noparse>}</#noparse>
    </select>

    <!-- insert -->
    <insert id="insert" useGeneratedKeys="true" keyProperty="${pk.columnName}" parameterType="${package}.${moduleName}.model.${className}" >
        INSERT INTO ${tableName} (<include refid="allColumns" />)
            VALUES(
        <#if columns?? &&columns?size gt 0>
            <#list columns as column>
                <#if column_index+1<columns?size><#noparse>#{</#noparse>${column.attrname},jdbcType=${column.dataType}<#noparse>}</#noparse>,<#else><#noparse>#{</#noparse>${column.attrname},jdbcType=${column.dataType}<#noparse>}</#noparse></#if>
            </#list>
        </#if>
        )
    </insert>

    <!-- update -->
    <update id="update" parameterType="${package}.${moduleName}.model.${className}" >
        UPDATE  ${tableName}
        SET
            <#if columns?? &&columns?size gt 0>
                <#list columns as column>
                    <#if column_index+1<columns?size && column.columnName?? && pk.columnName?? && column.columnName!=pk.columnName >
            ${column.columnName}=<#noparse>#{</#noparse>${column.attrname},jdbcType=${column.dataType}<#noparse>}</#noparse>,
                    <#elseif column_index+1==columns?size && column.columnName?? && pk.columnName?? && column.columnName!=pk.columnName >
            ${column.columnName}=<#noparse>#{</#noparse>${column.attrname},jdbcType=${column.dataType}<#noparse>}</#noparse>
                    </#if>
                </#list>
            </#if>
        WHERE ${pk.columnName}=<#noparse>#{</#noparse>${pk.attrname},jdbcType=${pk.dataType}<#noparse>}</#noparse>
    </update>

    <!-- batchDelete -->
    <delete id="batchDelete" parameterType="java.util.List">
        DELETE FROM  ${tableName}  WHERE ${pk.columnName}  IN
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            <#noparse>#{item.</#noparse>${pk.columnName},jdbcType=${pk.dataType}<#noparse>}</#noparse>
        </foreach>
    </delete>

</mapper>